<template>
  <!-- 父容器 -->
  <div class="container">
    <!-- 股票行家首页链接 -->
    <span @click="goToHome" class="home-link">股票行家首页</span> <!-- 用百分比定位 -->

    <!-- 图片容器 -->
    <div class="image-container">
      <img src="/images/logo2.png" alt="Logo 2" class="logo2" />
      <img src="/images/22.png" alt="Logo" class="logo1" />
    </div>

    <!-- 登录容器 -->
    <div class="login-container">
      <div class="login-switch">
        <span @click="toggleLoginType('sms')" :class="{ 'active': loginType === 'sms' }">短信登录</span>
        <span @click="toggleLoginType('password')" :class="{ 'active': loginType === 'password' }">密码登录</span>
      </div>

      <div class="login-content">
        <div v-if="loginType === 'sms'" class="sms-login">
          <el-input v-model="smsPhone" style="height: 38px;" placeholder="请输入手机号" prefix-icon="el-icon-mobile"></el-input>
          <div class="sms-code">
            <el-input v-model="smsCode" style="height: 38px;" placeholder="验证码" prefix-icon="el-icon-message"></el-input>
            <el-button @click="sendSmsCode" v-if="controlSendPhoneConfirm===false" style="height: 38px;margin-left: 13px;" class="sms-button">获取验证码</el-button>
            <div class="codeArea" v-if="controlSendPhoneConfirm===true">{{cunt_time}}</div>
          </div>
        </div>

        <div v-else class="password-login">
          <el-input v-model="passwordPhone" style="height: 38px" placeholder="请输入用户名" prefix-icon="el-icon-user"></el-input>
          <div class="password-spacing"></div>
          <el-input v-model="password" type="password" style="height: 38px;" placeholder="请输入密码" prefix-icon="el-icon-lock"></el-input>
          <div style="margin-top: 17px;display: flex">
            <el-input v-model="passwordVerCode" placeholder="请输入验证码" class="passwordVerCode"></el-input>
            <img class="VerImg" @click="getVorderImg" :src="VorderImg">
          </div>
          <el-checkbox v-model="rememberMe">7天免密登录</el-checkbox>
        </div>

        <el-button type="primary" @click="handleLogin" class="login-button">登录</el-button>

        <div class="login-footer">
          <span @click="register">注册账号</span>
          <span v-if="loginType === 'password'" @click="forgotPassword">忘记密码</span>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { ref } from 'vue';
import { useRouter } from 'vue-router'; // 导入 useRouter
import { ElMessage } from 'element-plus';
import {getConfirmCodeService,userLoginByPhoneService, getLoginImg, getUserMessage, userLoginService} from '@/api/request.js'; // 引入后端接口服务函数
import {useCounterStore} from "@/stores/counter.js"

export default {
  setup() {
    const store = useCounterStore();
    const router = useRouter(); // 使用 useRouter
    const loginType = ref('sms');
    const smsPhone = ref('');
    const smsCode = ref('');
    const passwordPhone = ref('');
    const password = ref('');
    const rememberMe = ref(false);

    // 密码登录验证码
    const passwordVerCode = ref('');

    const toggleLoginType = (type) => {
      loginType.value = type;
    };

    let controlSendPhoneConfirm=ref(false)
    let cunt_time=ref(30)
    const sendSmsCode = async () => {
      if (!smsPhone.value) {
        ElMessage.error('请输入手机号');
        return;
      }
      controlSendPhoneConfirm.value=true
      await getConfirmCodeService(smsPhone.value)
      let timer = setInterval(()=>{
        cunt_time.value--
        if(cunt_time.value===0){
          clearInterval(timer)
          controlSendPhoneConfirm.value=false
          cunt_time.value=30
        }
      },1000)
    };

    let VorderImg = ref('')

    async function getVorderImg() {
      VorderImg.value = await getLoginImg()
    }
    getVorderImg()
    // console.log(getLoginImg())


    const handleLogin = async () => {
      try {

        let response;
        if (loginType.value === 'sms') {
          if(!smsPhone.value) {
            ElMessage.error('请输入手机号');
            return;
          }
          if(!smsCode.value) {
            ElMessage.error('请输入验证码');
            return;
          }
          response = await userLoginByPhoneService(smsPhone.value, smsCode.value);
        } else {
          if (!passwordVerCode.value) {
            ElMessage.error('请输入验证码');
            return;
          }
          if(!passwordPhone.value) {
            ElMessage.error('请输入手机号');
            return;
          }
          if(!password.value) {
            ElMessage.error('请输入密码');
            return;
          }
          response = await userLoginService({
            phone: passwordPhone.value,
            password: password.value,
            captcha: passwordVerCode.value
          });
        }

        sessionStorage.setItem('token', response);
        let userMessage=await getUserMessage()
        switch (userMessage.gender) {
          case '0':
            userMessage.gender = '男'
            break;
          case '1':
            userMessage.gender = '女'
            break;
          default:
            userMessage.gender = '未知'
            break;
        }
        store.UserMessage=userMessage
        store.loginStatus=true
        ElMessage.success(response.message || '登录成功');
        router.push({ path: '/' });

      } catch (error) {
        ElMessage.error('登录失败，请重试');
      }
    };

    const register = () => {
      // ElMessage.info('跳转到注册页面');
      router.push({ path: '/register' });
    };

    const forgotPassword = () => {
      // ElMessage.info('跳转到忘记密码页面');
      router.push({ path: '/lost' });
    };

    const goToHome = () => {
      router.push({ path: '/' }); // 跳转到首页
    };

    return {
      VorderImg,
      controlSendPhoneConfirm,
      cunt_time,
      passwordVerCode,
      loginType,
      smsPhone,
      smsCode,
      passwordPhone,
      password,
      rememberMe,
      toggleLoginType,
      sendSmsCode,
      handleLogin,
      register,
      forgotPassword,
      goToHome,
      getVorderImg,
    };
  }
};
</script>

<style scoped>
.container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  position: relative;
}

.home-link {
  position: absolute;
  top: 5%;
  right: 5%;
  color: #409eff;
  cursor: pointer;
  font-size: 14px;
  text-decoration: none;
}

.image-container {
  width: 600px;
  height: 700px;
  display: flex;
  flex-direction: column;
  align-items: center;
}

.logo2 {
  width: 300px;
  height: 90px;
  margin-bottom: 10px;
  align-self: flex-start;
}

.logo1 {
  width: 600px;
  height: 500px;
  object-fit: cover;
  border-radius: 8px;
}

.login-container {
  width: 400px;
  height: 415px;
  padding: 40px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  border: 2px solid #ebeef5;
  border-radius: 8px;
  background-color: #fff;
}

.login-switch {
  display: flex;
  justify-content: space-around;
  padding-bottom: 10px;
  font-size: 16px;
}

.login-switch span {
  cursor: pointer;
  font-weight: bold;
}

.login-switch .active {
  color: #409eff;
}

.login-content {
  width: 100%;
}

.sms-login,
.password-login {
  margin-bottom: 10px;
}

.sms-code {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: 20px;
  position: relative;
}

.sms-code .el-input {
  flex: 0 1 74%;
}

.sms-button {
  flex: 0 1 24%;
}

.login-button {
  width: 100%;
  height: 40px;
  line-height: 40px;
  text-align: center;
  font-weight: bold;
  margin-top: 20px;
}

.password-spacing {
  height: 20px;
}

.login-footer {
  display: flex;
  justify-content: space-between;
  margin-top: 10px;
  font-size: 14px;
}

.login-footer span {
  color: #409eff;
  cursor: pointer;
}
.passwordVerCode{
  width: 66%;
  height: 47px;
}
.VerImg{
  width: 107px;
  height: 46px;
  margin-left: 15px;
  cursor: pointer;
}
.codeArea{
  position: absolute;
  right: 70px;
}
</style>
